package com.leoblog.interceptors;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.leoblog.constants.Constants;
import com.leoblog.util.UrlUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class CommonDataSetInterceptor extends AbstractInterceptor {

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		ActionContext ctx = invocation.getInvocationContext();
		// Get the HttpServletRequest.
		HttpServletRequest request= 
			(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST );
		// Get request parameters.
		Map parameters = ctx.getParameters();
		// Get previous url.
		String wholeURL = request.getHeader("Referer");
		String contextPath = request.getContextPath();
		// Get relative url.
		String url = UrlUtil.getRelativeURL(contextPath, wholeURL);
		// Set previous url into request parameters.
		parameters.put(Constants.BACK_URL_KEY, url);
		// Set back the parameters into request.
		ctx.setParameters(parameters);

        return invocation.invoke();
	}

}
